﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Areas.Veggies.Controllers
{
    public class FruitAjaxDropdownController : Controller
    {
        private readonly IRepo<Fruit> r;

        public FruitAjaxDropdownController(IRepo<Fruit> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(int? key)
        {
            var list = new List<SelectListItem> { new SelectListItem { Text = "not selected", Value = "" } };
            list.AddRange(r.GetAll().Select(o => new SelectListItem
            {
                Text = o.Name,
                Value = o.Id.ToString(),
                Selected = o.Id == key
            }));
            return Json(list);
        }
    }
}
